package com.itboye.mengwu.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.inageview.RoundImageView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.MyZengSongPingJiaBean;
import com.itboye.mengwu.data.model.ShopPingJiabean;
import com.itboye.mengwu.widget.GridViewNameAdapter;

public class ShopShngPingAdapter extends BaseAdapter {
	private Context mContext;
	private MyZengSongPingJiaBean items;
	private OnClickListener clickListener;

	public ShopShngPingAdapter(Context mContext, MyZengSongPingJiaBean items) {
		this.items = items;
		this.mContext = mContext;
	}

	public void upDate(FragmentActivity activity, MyZengSongPingJiaBean arrlistTwo) {
		this.items = items;
		this.mContext = mContext;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.getList() == null ? 0 : items.getList().size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		// houderView holder;
		houderView holder;
		if (convertView == null) {
			holder = new houderView();
			convertView = View.inflate(mContext, R.layout.item_mepingjia, null);
			holder.imgMePingJia = (RoundImageView) convertView.findViewById(R.id.imgMePingJia);
			holder.tv_Context = (TextView) convertView.findViewById(R.id.tv_Context);
			holder.txtPinJiaTime = (TextView) convertView.findViewById(R.id.txtPinJiaTime);
			holder.gridview = (NoScrollGridView) convertView.findViewById(R.id.mepingjiagridview);
			holder.img1 = (ImageView) convertView.findViewById(R.id.img1);

			holder.img2 = (ImageView) convertView.findViewById(R.id.img2);

			holder.img1 = (ImageView) convertView.findViewById(R.id.img1);

			holder.img3 = (ImageView) convertView.findViewById(R.id.img3);

			holder.img1 = (ImageView) convertView.findViewById(R.id.img1);

			holder.img4 = (ImageView) convertView.findViewById(R.id.img4);

			holder.img5 = (ImageView) convertView.findViewById(R.id.img5);

			//

			holder.img6 = (ImageView) convertView.findViewById(R.id.img6);

			holder.img7 = (ImageView) convertView.findViewById(R.id.img7);

			holder.img8 = (ImageView) convertView.findViewById(R.id.img8);

			holder.img9 = (ImageView) convertView.findViewById(R.id.img9);

			holder.img10 = (ImageView) convertView.findViewById(R.id.img10);

			//
			holder.img11 = (ImageView) convertView.findViewById(R.id.img11);

			holder.img12 = (ImageView) convertView.findViewById(R.id.img12);

			holder.img13 = (ImageView) convertView.findViewById(R.id.img13);

			holder.img14 = (ImageView) convertView.findViewById(R.id.img14);

			holder.img15 = (ImageView) convertView.findViewById(R.id.img15);
			convertView.setTag(holder);
		} else {
			holder = (houderView) convertView.getTag();
		}
		// ArrayList<MePin> itemEntity =ArrayList<MePin>();
		holder.tv_Context.setText("\t\t\t\t\t\t\t\t\t\t\t\t" + items.getList().get(position).getEvaluateContent());
		holder.txtPinJiaTime.setText(new SimpleDateFormat("yyyy-MM-dd")
				.format(Long.parseLong(items.getList().get(position).getWinTime()) * 1000));
		XImageLoader.load(SPUtils.get(mContext, null, SpPublicParams.USERHEAD, "") + "", holder.imgMePingJia);

		// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" +
		// items.getList().get(position).getComment());

		final ArrayList<String> imageUrls = (ArrayList<String>) items.getList().get(position).getEvaluateImgs();
		if (imageUrls == null || imageUrls.size() == 0) { // 没有图片资源就隐藏GridView
			holder.gridview.setVisibility(View.GONE);
		} else {
			holder.gridview.setAdapter(new GridViewNameAdapter(mContext, imageUrls));
			holder.gridview.setVisibility(View.VISIBLE);
		}
		// 点击回帖九宫格，查看大图
		holder.gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				imageBrower(position, imageUrls);
			}
		});

		holder.img15.setTag(arg2);
		holder.img14.setTag(arg2);
		holder.img13.setTag(arg2);
		holder.img12.setTag(arg2);
		holder.img11.setTag(arg2);
		holder.img10.setTag(arg2);
		holder.img9.setTag(arg2);
		holder.img8.setTag(arg2);
		holder.img7.setTag(arg2);
		holder.img6.setTag(arg2);
		holder.img5.setTag(arg2);
		holder.img4.setTag(arg2);
		holder.img3.setTag(arg2);
		holder.img2.setTag(arg2);
		holder.img1.setTag(arg2);
		holder.img1.setOnClickListener(clickListener);
		holder.img2.setOnClickListener(clickListener);
		holder.img3.setOnClickListener(clickListener);
		holder.img4.setOnClickListener(clickListener);
		holder.img5.setOnClickListener(clickListener);
		holder.img6.setOnClickListener(clickListener);
		holder.img7.setOnClickListener(clickListener);
		holder.img8.setOnClickListener(clickListener);
		holder.img9.setOnClickListener(clickListener);
		holder.img10.setOnClickListener(clickListener);
		holder.img11.setOnClickListener(clickListener);
		holder.img12.setOnClickListener(clickListener);
		holder.img13.setOnClickListener(clickListener);
		holder.img14.setOnClickListener(clickListener);
		holder.img15.setOnClickListener(clickListener);

		if (Double.parseDouble(items.getList().get(position).getScore1()) == 1) {
			holder.img1.setImageResource(R.drawable.like_select);
			holder.img1.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore1()) == 2) {
			holder.img2.setImageResource(R.drawable.like_select);
			holder.img1.setImageResource(R.drawable.like_select);
			holder.img1.setTag(R.id.tag_first, "1");
			holder.img2.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore1()) == 3) {
			holder.img1.setImageResource(R.drawable.like_select);
			holder.img2.setImageResource(R.drawable.like_select);
			holder.img3.setImageResource(R.drawable.like_select);
			holder.img1.setTag(R.id.tag_first, "1");
			holder.img2.setTag(R.id.tag_first, "1");
			holder.img3.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore1()) == 4) {
			holder.img1.setImageResource(R.drawable.like_select);
			holder.img2.setImageResource(R.drawable.like_select);
			holder.img3.setImageResource(R.drawable.like_select);
			holder.img1.setTag(R.id.tag_first, "1");
			holder.img2.setTag(R.id.tag_first, "1");
			holder.img3.setTag(R.id.tag_first, "1");
			holder.img4.setImageResource(R.drawable.like_select);
			holder.img4.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore1()) == 5) {
			holder.img1.setImageResource(R.drawable.like_select);
			holder.img2.setImageResource(R.drawable.like_select);
			holder.img3.setImageResource(R.drawable.like_select);
			holder.img1.setTag(R.id.tag_first, "1");
			holder.img2.setTag(R.id.tag_first, "1");
			holder.img3.setTag(R.id.tag_first, "1");
			holder.img4.setImageResource(R.drawable.like_select);
			holder.img4.setTag(R.id.tag_first, "1");
			holder.img5.setImageResource(R.drawable.like_select);
			holder.img5.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore1()) == 0) {

		}
		if (Double.parseDouble(items.getList().get(position).getScore2()) == 1) {
			holder.img6.setImageResource(R.drawable.like_select);
			holder.img6.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore2()) == 2) {
			holder.img6.setImageResource(R.drawable.like_select);
			holder.img6.setTag(R.id.tag_first, "1");
			holder.img7.setImageResource(R.drawable.like_select);
			holder.img7.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore2()) == 3) {
			holder.img6.setImageResource(R.drawable.like_select);
			holder.img6.setTag(R.id.tag_first, "1");
			holder.img7.setImageResource(R.drawable.like_select);
			holder.img7.setTag(R.id.tag_first, "1");
			holder.img8.setImageResource(R.drawable.like_select);
			holder.img8.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore2()) == 4) {
			holder.img6.setImageResource(R.drawable.like_select);
			holder.img6.setTag(R.id.tag_first, "1");
			holder.img7.setImageResource(R.drawable.like_select);
			holder.img7.setTag(R.id.tag_first, "1");
			holder.img8.setImageResource(R.drawable.like_select);
			holder.img8.setTag(R.id.tag_first, "1");
			holder.img9.setImageResource(R.drawable.like_select);
			holder.img9.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore2()) == 5) {
			holder.img6.setImageResource(R.drawable.like_select);
			holder.img6.setTag(R.id.tag_first, "1");
			holder.img7.setImageResource(R.drawable.like_select);
			holder.img7.setTag(R.id.tag_first, "1");
			holder.img8.setImageResource(R.drawable.like_select);
			holder.img8.setTag(R.id.tag_first, "1");
			holder.img9.setImageResource(R.drawable.like_select);
			holder.img9.setTag(R.id.tag_first, "1");
			holder.img10.setImageResource(R.drawable.like_select);
			holder.img10.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore2()) == 0) {

		}
		if (Double.parseDouble(items.getList().get(position).getScore3()) == 1) {
			holder.img11.setImageResource(R.drawable.like_select);
			holder.img11.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore3()) == 2) {
			holder.img11.setImageResource(R.drawable.like_select);
			holder.img11.setTag(R.id.tag_first, "1");
			holder.img12.setImageResource(R.drawable.like_select);
			holder.img12.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore3()) == 3) {
			holder.img11.setImageResource(R.drawable.like_select);
			holder.img11.setTag(R.id.tag_first, "1");
			holder.img12.setImageResource(R.drawable.like_select);
			holder.img12.setTag(R.id.tag_first, "1");
			holder.img13.setImageResource(R.drawable.like_select);
			holder.img13.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore3()) == 4) {
			holder.img11.setImageResource(R.drawable.like_select);
			holder.img11.setTag(R.id.tag_first, "1");
			holder.img12.setImageResource(R.drawable.like_select);
			holder.img12.setTag(R.id.tag_first, "1");
			holder.img13.setImageResource(R.drawable.like_select);
			holder.img13.setTag(R.id.tag_first, "1");
			holder.img14.setImageResource(R.drawable.like_select);
			holder.img14.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore3()) == 5) {
			holder.img11.setImageResource(R.drawable.like_select);
			holder.img11.setTag(R.id.tag_first, "1");
			holder.img12.setImageResource(R.drawable.like_select);
			holder.img12.setTag(R.id.tag_first, "1");
			holder.img13.setImageResource(R.drawable.like_select);
			holder.img13.setTag(R.id.tag_first, "1");
			holder.img14.setImageResource(R.drawable.like_select);
			holder.img14.setTag(R.id.tag_first, "1");
			holder.img15.setImageResource(R.drawable.like_select);
			holder.img15.setTag(R.id.tag_first, "1");
		} else if (Double.parseDouble(items.getList().get(position).getScore3()) == 0) {

		}

		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}

	class houderView {
		private TextView txtYiPinJia, txtPinJiaTime, txtMiaoshu, txtWuLiu, txtZengSong;
		private RoundImageView imgMePingJia;
		private NoScrollGridView mepingjiagridview;

		// ///
		private ImageView iv_avatar;
		private TextView tv_Context;
		private TextView tv_content;
		private NoScrollGridView gridview;

		private ImageView img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14,
				img15;
	}

}
